package com.xmkj.bearbiz.member;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.common.BearBean.MemberInfoBean;
import com.common.BearBean.ShopBean;
import com.common.BearMethods.GetShopDatasMethods;
import com.common.BearMethods.QueryMemberInfoMethods;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.xmkj.bearbiz.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Create by leo
 * 创建日期：2018/6/13
 * 描述：
 */

public class MemberFragment extends BaseMvpFragment {
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.tv_quary)
    TextView tvQuary;
    @BindView(R.id.iv_next)
    ImageView ivNext;
    @BindView(R.id.tv_condition)
    TextView tvCondition;
    @BindView(R.id.rl_choose)
    RelativeLayout rlChoose;
    @BindView(R.id.day)
    RadioButton day;
    @BindView(R.id.week)
    RadioButton week;
    @BindView(R.id.mouth)
    RadioButton mouth;
    @BindView(R.id.year)
    RadioButton year;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.progress1)
    ProgressBar progress1;
    @BindView(R.id.tv_active)
    TextView tvActive;
    @BindView(R.id.progress2)
    ProgressBar progress2;
    @BindView(R.id.tv_new)
    TextView tvNew;
    @BindView(R.id.progress3)
    ProgressBar progress3;
    @BindView(R.id.tv_consume)
    TextView tvConsume;
    @BindView(R.id.iv_add_member_list)
    ImageView ivAddMemberList;
    Unbinder unbinder;
    @BindView(R.id.total_people)
    TextView totalPeople;
    @BindView(R.id.total_money)
    TextView totalMoney;
    @BindView(R.id.total_active)
    TextView totalActive;
    @BindView(R.id.new_people)
    TextView newPeople;
    Unbinder unbinder1;
    private List<String> outDatas = new ArrayList<>();
    private List<String> inDatas = new ArrayList<>();
    private List<List<String>> inListDatas = new ArrayList<>();
    private OptionsPickerView siteTypePicker;
    private ArrayList<ShopBean> shopBeans;
    private String shopCode;
    private int timeType = 1;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_member;
    }

    @Override
    protected void onViewCreated(View view) {

    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(ivAddMemberList);
        attachClickListener(llSearch);
        attachClickListener(rlChoose);
        attachClickListener(tvQuary);
        getShopDatas();
        radioGroup.check(day.getId());
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.day:
                        timeType = 1;  // 微信
                        break;
                    case R.id.week:
                        timeType = 2;  //支付宝
                        break;
                    case R.id.mouth:
                        timeType = 3;
                        break;
                    case R.id.year:
                        timeType = 4;
                        break;
                }
            }
        });

    }

    private void getShopDatas() {
        CommonSubscriber<ArrayList<ShopBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                shopBeans = (ArrayList<ShopBean>) o;
                initTypeOptionsPicker();

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        GetShopDatasMethods.getInstance().getShop(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID));
        rxManager.add(subscriber);
    }

    private void initDatas() {
   /*     for (int i = 0; i < 3; i++) {
            outDatas.add("店铺" + i);
            for (int j = 0; j < 4; j++) {
                if(j==3){
                    inListDatas.add(inDatas);
                    inDatas = null;
                    inDatas = new ArrayList<>();
                }
                inDatas.add("商品"+j);
            }
        }*/
        if (shopBeans != null && shopBeans.size() > 0) {
            for (int i = 0; i < shopBeans.size(); i++) {
                outDatas.add(shopBeans.get(i).region);
                for (int j = 0; j < shopBeans.get(i).lists.size(); j++) {
                    inDatas.add(shopBeans.get(i).lists.get(j).clientname);
                }
                inListDatas.add(inDatas);
                inDatas = null;
                inDatas = new ArrayList<>();
            }
        }


    }

    @Override
    protected void onViewClicked(View view) {
        if(ivAddMemberList.getId()==view.getId()){
            Intent intent = new Intent(context,SearchAndMemberListActivity.class);
            intent.putExtra(SearchAndMemberListActivity.TYPE,"2");
//            intent.putExtra(SearchAndMemberListActivity.CLIENTCODE,shopCode);
            if(EmptyUtils.isEmpty(shopCode)){
                intent.putExtra(SearchAndMemberListActivity.CLIENTCODE,shopBeans.get(0).lists.get(0).clientcode);
            }else {
                intent.putExtra(SearchAndMemberListActivity.CLIENTCODE,shopCode);

            }
            context.startActivity(intent);
        } else if (view.getId() == llSearch.getId()) {

            Intent intent = new Intent(context,SearchAndMemberListActivity.class);
            intent.putExtra(SearchAndMemberListActivity.TYPE,"1");
//            intent.putExtra(SearchAndMemberListActivity.CLIENTCODE,shopCode);
            intent.putExtra(SearchAndMemberListActivity.CLIENTCODE,"");
            context.startActivity(intent);
        } else if (view.getId() == rlChoose.getId()) {
            if (siteTypePicker != null) {
                siteTypePicker.show();
            }
        } else if (view.getId() == tvQuary.getId()) {
            if (EmptyUtils.isEmpty(shopCode)) {
                showToastMsg("请选择店铺！");
                return;
            }
            gotoQueryUserDetails();
        }
    }

    private void gotoQueryUserDetails() {
        showProgressingDialog();
        CommonSubscriber<MemberInfoBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                MemberInfoBean memberInfoBean = (MemberInfoBean) o;
                totalPeople.setText(memberInfoBean.total_people+"人");
                totalMoney.setText(memberInfoBean.total_money+"元");
                totalActive.setText(memberInfoBean.total_active+"人");
                newPeople.setText(memberInfoBean.new_people+"人");
                tvActive.setText(memberInfoBean.active_proportion);
                tvConsume.setText(memberInfoBean.money_proportion);
                tvNew.setText(memberInfoBean.newpeople_proportion);
                int active = Double.valueOf(memberInfoBean.active_proportion.replace("%","")).intValue();
                progress1.setProgress(active);
                int Condition = Double.valueOf(memberInfoBean.newpeople_proportion.replace("%","")).intValue();
                progress2.setProgress(Condition);
                int Consume = Double.valueOf(memberInfoBean.money_proportion.replace("%","")).intValue();
                progress3.setProgress(Consume);


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        QueryMemberInfoMethods.getInstance().vipinfo(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID), shopCode, timeType + "");
        rxManager.add(subscriber);
    }

    private void initTypeOptionsPicker() {

        initDatas();

        siteTypePicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                shopCode = shopBeans.get(options1).lists.get(options2).clientcode;
                tvCondition.setText(shopBeans.get(options1).lists.get(options2).clientname);

            }
        }).setSubmitText(getString(R.string.insure)).
                setCancelText(getString(R.string.cacal)).
                build();
        siteTypePicker.setPicker(outDatas, inListDatas);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void bindKnife() {
        unbinder = ButterKnife.bind(this, view);
    }


}
